package com.hntx.joymusic.record;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;

public class TalAllow {
    /**
     * RECORD_AUDIO     音频权限
     * WRITE_EXTERNAL_STORAGE 写入权限
     * CAMERA        相机权限
     */
    public static void requestPermissions(Context context, int allowCode) {
        ArrayList<String> ps = new ArrayList<>();
        int per = ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO);
        if (per != PackageManager.PERMISSION_GRANTED) {
            ps.add(Manifest.permission.RECORD_AUDIO);
        }
        per = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (per != PackageManager.PERMISSION_GRANTED) {
            ps.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        per = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
        if (per != PackageManager.PERMISSION_GRANTED) {
            ps.add(Manifest.permission.CAMERA);
        }
        per = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE);
        if (per != PackageManager.PERMISSION_GRANTED) {
            ps.add(Manifest.permission.READ_PHONE_STATE);
        }
        if (!ps.isEmpty()) {
            String[] ps3 = new String[ps.size()];
            ps.toArray(ps3);
            ActivityCompat.requestPermissions((Activity) context, ps3, allowCode);
        }
    }
}
